<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 资源位操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/contentHandler.class.php');

class pa_article {

    public function execute() {

        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //文章分类列表
                    $this->getArticleCategoryList();
                    break;
                case 2: //添加文章分类
                	$this->addArticleCategory();
                	break;
                case 3: //编辑文章分类
                	$this->editArticleCategory();
                	break;
                case 4: //获取文章列表
                	$this->getArticleList();
                	break;
                case 5: //添加文章
               	case 6: //编辑文章
                	$this->getArticle();
                	break;
                case 7: //文章评论列表
                    $this->articleCommentsList();
                    break;
                case 8: //文章回复
                    $this->articleReply();
                    break;
            }
        }
    }

    /**
     * 文章分类列表
     * @author raohenglei
     * 
     */
    private function getArticleCategoryList() {

        global $G_PGLOB;

        $G_PGLOB->smarty->display('content/articleCategoryList.tpl');

    }



    /**
     * 添加文章分类
     * @author raohenglei
     *
     */
    private function addArticleCategory () {
    	
    	global $G_PGLOB;

        $G_PGLOB->smarty->assign('operFlg', $_REQUEST['operFlg']);
        $G_PGLOB->smarty->display('content/articleCate.tpl');
		
    }
	
    /**
     * 修改资源位分类
     */
    public function editArticleCategory() {

        global $G_PGLOB;

        $G_PGLOB->smarty->assign('operFlg', $_REQUEST['operFlg']);
		
        if (!isset($_REQUEST['category_id']) || !is_numeric($_REQUEST['category_id'])) {
            $G_PGLOB->smarty->assign('error_msg', '缺少参数');
            $G_PGLOB->smarty->display('content/articleCategory.tpl');
            die;
        }

        //实例化资源位类
        $contentHandler = new contentHandler( $G_PGLOB->DBCA );

        //查询修改的资源位分类信息
        $arg_array = array(
            'category_id' => $_REQUEST['category_id']
        );
        $article_category_array = $contentHandler->searchArticleCategory( $arg_array );


        if ($article_category_array) {

            $G_PGLOB->smarty->assign_by_ref('article_category_array', $article_category_array[0]);

        } else {

            $G_PGLOB->smarty->assign('error_msg', '文章分类信息没有找到');

        }

        $G_PGLOB->smarty->display('content/articleCate.tpl');

    }
    
    /**
     * 文章列表
     */
    private function getArticleList () {

        global $G_PGLOB;

		//实例化文章类
    	$contentHandler = new contentHandler( $G_PGLOB->DBCA );

        //查询文章分类
        $arg_array = array(
            'sortname' => 'category_id',
            'sortorder' => 'DESC'
        );
		$category_array = $contentHandler->searchArticleCategory( $arg_array );
		$G_PGLOB->smarty->assign_by_ref('category_array', $category_array);
        
        $G_PGLOB->smarty->display('content/articleList.tpl');

    }


    /**
     * 添加或修改文章
     * @throws Exception
     */
    private function getArticle() {

		global $G_PGLOB;

		try {

            if ($_REQUEST['operFlg'] == 6 && (!isset($_REQUEST['article_id']) || !is_numeric($_REQUEST['article_id'])))
                throw new Exception('参数错误', 400);
			
			//实例化文章类
            $contentHandler = new contentHandler( $G_PGLOB->DBCA );

            //查询文章分类
            $arg_array = array(
                'useFlg' => 1,
                'sortname' => 'category_id',
                'sortorder' => 'DESC'
            );
            $category_array = $contentHandler->searchArticleCategory( $arg_array );
            if (!$category_array)
                throw new Exception('文章分类信息没有找到，请先添加文章分类', 404);

            //查询文章信息
            if (isset($_REQUEST['article_id'])) {

                $arg_array = array(
                    'article_id' => $_REQUEST['article_id']
                );
                $article_array = $contentHandler->searchArticle( $arg_array );
                if (!$article_array)
                    throw new Exception('文章信息没有找到', 404);

                $G_PGLOB->smarty->assign_by_ref('article_array', $article_array[0]);

            }

            $G_PGLOB->smarty->assign_by_ref('category_array', $category_array);
			
			$G_PGLOB->smarty->assign('operFlg', $_REQUEST['operFlg']);

		} catch(Exception $e) {

            if ($e->getCode() > 0) {
                $G_PGLOB->smarty->assign('error_msg', $e->getMessage());
            } else {
                error_log($e->getMessage());
                $G_PGLOB->smarty->assign('error_msg', '系统错误，请与系统管理员联系');
            }

		}

        $G_PGLOB->smarty->display('content/article.tpl');

	}

    //文章评论列表
    function articleCommentsList(){

        global $G_PGLOB;

        $G_PGLOB->smarty->display('content/articleCommentsList.tpl');
    }

    //文章评论回复
    function articleReply(){

        global $G_PGLOB;

        //接收参数
        $uuid = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : '';

        if( empty($uuid) ){
            $G_PGLOB->smarty->assign('msg', '参数错误');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        //查询信息
        $article_comment_obj = new entity('content_article_comments');
        $article_comment_obj->uuid = $uuid;

        if( !$G_PGLOB->DBCA->fetch($article_comment_obj) ){
            $G_PGLOB->smarty->assign('msg', '没有找到信息');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        $G_PGLOB->smarty->assign('articleCommentInfo' , $article_comment_obj);
        $G_PGLOB->smarty->display('content/articleReply.tpl');
    }

}